#include "mainwindow.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>

MainWindow::MainWindow(QWidget* parent) : QMainWindow (parent), sendVisual(true){
    setupUI();
    setupDconfig();
    // 加载 GSettings 配置，读取 output 的显示状态,监听 GSettings变化
    gsettings = new QGSettings("com.mytest.gsettings");
    outputVisual = gsettings->get("outputvisual").toBool();
    connect(gsettings, &QGSettings::changed, this, [this](const QString &key) {
        if (key == "outputvisual") {
            outputVisual = gsettings->get("outputvisual").toBool();
            updateOutput();
        }
    });
    // 监听DConfig变化
    connect(dConfig, &Dtk::Core::DConfig::valueChanged, this, &MainWindow::updateSend);
}
void MainWindow::setupUI(){
    this->setWindowTitle("Dbus-Client");
    this->resize(400, 300);

    send = new QPushButton("send", this);
    output = new QTextEdit(this);
    output->setReadOnly(true);
    name = new QLabel("姓名");
    nameInput = new QLineEdit("熊大",this);
    age = new QLabel("年龄");
    ageInput = new QLineEdit("30",this);
    phone = new QLabel("电话");
    phoneInput = new QLineEdit("17788899972",this);
    cid = new QLabel("身份证号");
    cidInput = new QLineEdit("420111199410101010", this);
    home = new QLabel("住址");
    homeInput = new QLineEdit("宿舍", this);
    tem = new QLabel("体温");
    temInput = new QLineEdit("38",this);
    QFormLayout* formLayout = new QFormLayout();
    formLayout->addRow(name, nameInput);
    formLayout->addRow(age, ageInput);
    formLayout->addRow(phone, phoneInput);
    formLayout->addRow(cid, cidInput);
    formLayout->addRow(home, homeInput);
    formLayout->addRow(tem, temInput);

    QHBoxLayout* headLayout = new QHBoxLayout();
    QVBoxLayout* mainLayout = new QVBoxLayout();
    mainLayout->addLayout(formLayout);
    headLayout->addWidget(send);
    mainLayout->addLayout(headLayout);
    mainLayout->addWidget(output);

    QWidget* centralWidget = new QWidget(this);
    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);

    connect(send, &QPushButton::clicked, this, &MainWindow::buttonClicked);

}

void MainWindow::buttonClicked(){
    saveGsettings();
    // 信息以json格式保存，转换为字符串传输
    QJsonObject json;
    json["name"] = nameInput->text();
    json["age"] = ageInput->text();
    json["phone"] = phoneInput->text();
    json["cid"] = cidInput->text();
    json["home"] = homeInput->text();
    json["tem"] = temInput->text();
    QJsonDocument jsonDoc(json);
    QString message = jsonDoc.toJson(QJsonDocument::Compact);
    output->append("Sent: " + message);
    // 使用 QDBusInterface 调用服务端方法
    QDBusInterface iface(serviceServerName,
                         serviceServerPath,
                         serviceServerInterface,
                         QDBusConnection::sessionBus());

    if (iface.isValid()) {
        QDBusMessage reply = iface.call("receiveMessage", message);
        if (reply.type() == QDBusMessage::ErrorMessage) {
            output->append("Error: " + reply.errorMessage());
        } else {
            output->append("Message sent successfully.");
        }
    } else {
        output->append("Failed to connect to the D-Bus service.");
    }
}

void MainWindow::saveGsettings(){
    gsettings->set("name", nameInput->text());
    gsettings->set("age", ageInput->text());
    gsettings->set("cid", cidInput->text());
    gsettings->set("phone", phoneInput->text());
    gsettings->set("home", homeInput->text());
    gsettings->set("tem", temInput->text());
}


void MainWindow::updateSend(){
    sendVisual = !sendVisual;
    send->setVisible(sendVisual);
}
void MainWindow::updateOutput(){
    output->setVisible(outputVisual);
}

void MainWindow::setupDconfig(){
    int sendVisual = dConfig->value("sendVisual").toInt();
    // 如果值为 1，显示按钮；否则隐藏
    send->setVisible(sendVisual == 1);
}
